package com.monolithiot.sso.common.vo;

import com.monolithiot.sso.common.data.JSONSerializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 2018/11/12 Create by 郭文梁
 * BasicJsonResponse
 * 统一包装JSON返回数据格式，统一格式为:
 * <p>
 * <pre>
 *     <code>
 * {
 *     "code":0, //返回码
 *     "msg":"OK", //返回消息
 *     "data" : ... //返回数据
 * }
 *     </code>
 * </pre>
 * <p>
 * <p>
 * code规则：
 * code == 0 :系统正常返回
 * code < 0 :系统级异常，此时msg应当给出相应出错信息
 * code > 0 :输入参数有误，此时若系统有要求保密出错信息，msg可不提供信息
 *
 * @author 郭文梁
 * @date 2018/11/12
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuppressWarnings("all")
public class BasicJsonResponse<T> implements JSONSerializable {
    public static final int CODE_OK = 0;
    public static final int CODE_PARAM_ERROR = 1;
    public static final int CODE_SYSTEM_ERROR = -1;
    public static final String MSG_OK = "OK";
    public static final String MSG_PARAM_ERROR = "PARAM ERROR";
    public static final String MSG_SYSTEM_ERROR = "SYSTEM ERROR";

    public static <T> BasicJsonResponse<T> create(int code, String msg, T data) {
        return new BasicJsonResponse<>(code, msg, data);
    }

    public static <T> BasicJsonResponse<T> ok(T data) {
        return create(CODE_OK, MSG_OK, data);
    }

    public static <T> BasicJsonResponse<T> ok() {
        return create(CODE_OK, MSG_OK, null);
    }

    public static <T> BasicJsonResponse<T> paramError(int code, String msg) {
        return create(code, msg, null);
    }

    public static <T> BasicJsonResponse<T> paramError(String msg) {
        return paramError(CODE_PARAM_ERROR, msg);
    }

    public static <T> BasicJsonResponse<T> paramError() {
        return paramError(MSG_PARAM_ERROR);
    }

    public static <T> BasicJsonResponse<T> systemError(int code, String msg) {
        return create(code, msg, null);
    }

    public static <T> BasicJsonResponse<T> systemError(String msg) {
        return systemError(CODE_SYSTEM_ERROR, msg);
    }

    public static <T> BasicJsonResponse<T> systemError() {
        return systemError(MSG_SYSTEM_ERROR);
    }

    private int code = 0;
    private String msg = MSG_OK;
    private T data;
}
